<?php


namespace app\sonpartner\controller;

use redis\RedisPackage;

class SystemConfig extends Common
{
    /**
     * 个人资料
     */
    public function personalInfo()
    {
        $uid = $this -> uid;
        $info = db('user_manage')
            ->where('id', $uid)
            ->field('id as uid,headimgurl,xingming,code,sex,shengri,province,city')
            ->find();
        if(!empty($info)) {
            returnSuccess(['info'=>$info]);
        } else {
            returnError('用户信息查询失败');
        }
    }

    /**
     * 编辑个人资料
     */
    public function editInfo()
    {
        $uid = $this -> uid;
        $input = input();
        $xingming = isset($input['xingming']) ? $input['xingming'] : '';
        $sex = isset($input['sex']) ? $input['sex'] : 0;
        $shengri = isset($input['shengri']) ? $input['shengri'] : '';
        $province = isset($input['province']) ? $input['province'] : '';
        $city = isset($input['city']) ? $input['city'] : '';
        if(empty($xingming) || empty($shengri) || empty($province) || empty($city)) {
            returnError('缺少重要参数');
        }
        $param['xingming'] = $xingming;
        $param['sex'] = $sex;
        $param['shengri'] = $shengri;
        $param['province'] = $province;
        $param['city'] = $city;
        $result = db('user_manage')
            ->where('id', $uid)
            ->update($param);
        if($result) {
            returnSuccess([], '个人信息保存成功');
        } else {
            returnError('个人信息保存失败');
        }
    }

    /**
     * 账户安全修改密码
     */
    public function editPassword()
    {
        $id = $this -> id;
        $input = input();
        $oldpass = isset($input['oldpass']) ? $input['oldpass'] : '';
        $newpass = isset($input['newpass']) ? $input['newpass'] : '';
        if(empty($oldpass) || empty($newpass)) {
            returnError('缺少重要参数');
        }
        //验证用户
        $user = db('partner')
            ->where('id', $id)
            ->find();
        if(empty($user)) {
            returnError('该合伙人不存在');
        }
        if(md5($oldpass) != $user['password']) {
            returnError('原始密码输入错误');
        }
        if($oldpass != $newpass) {
            $result = db('partner')
                ->where('id', $id)
                ->update(['password'=>md5($newpass)]);
            if($result) {
                returnSuccess([], '密码修改成功');
            } else {
                returnError('密码修改失败');
            }
        } else {
            returnError('未作修改');
        }
    }

    /**
     * 更换绑定手机号
     */
    public function changeTel()
    {
        RedisPackage::getInstance();
        $input = input();
        $tel = isset($input['tel']) ? $input['tel'] : '';
        $smscode = isset($input['smscode']) ? $input['smscode'] : '';
        if(empty($tel) || empty($smscode)) {
            returnError('缺少重要参数');
        }
        $code = RedisPackage::get('code_' . $tel);
        if(empty($code)) {
            returnError('验证码已过期请重新发送');
        }
        if($code != $smscode) {
            returnError('验证码错误请重新输入');
        }
        RedisPackage::del('code_' . $tel);
        returnSuccess([], '验证成功');
    }

    /**
     * 确定更改手机号
     */
    public function confirmChange()
    {
        RedisPackage::getInstance();
        $uid = $this -> uid;
        $input = input();
        $newtel = isset($input['newtel']) ? $input['newtel'] : '';
        $smscode = isset($input['smscode']) ? $input['smscode'] : '';
        if(empty($newtel) || empty($smscode)) {
            returnError('缺少重要参数');
        }
        $code = RedisPackage::get('code_' . $newtel);
        if(empty($code)) {
            returnError('验证码已过期请重新发送');
        }
        if($code != $smscode) {
            returnError('验证码错误请重新输入');
        }
        RedisPackage::del('code_' . $newtel);
        $result = db('user_manage')
            ->where('id', $uid)
            ->update(['tel'=>$newtel]);
        if($result) {
            returnSuccess([], '手机号更改成功');
        } else {
            returnError('手机号更改失败');
        }
    }
}